package com.skd.mall.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.skd.mall.product.dao.AttrAttrgroupRelationDao;
import com.skd.mall.product.entity.AttrAttrgroupRelationEntity;
import com.skd.mall.product.entity.AttrEntity;
import com.skd.mall.product.service.AttrService;
import com.skd.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.Query;

import com.skd.mall.product.dao.AttrGroupDao;
import com.skd.mall.product.entity.AttrGroupEntity;
import com.skd.mall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    Query query = new Query();
    QueryWrapper wrapper = new QueryWrapper();
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                /*new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()*/
                query.getPage(params), wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 获取检索的关键字
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            // 拼接查询的条件
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if(catelogId == 0){
            // 不根据catelogId来查询
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),wrapper
            );
            return new PageUtils(page);
        }
        // 根据类别编号来查询属性信息
        wrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),wrapper
        );
        return new PageUtils(page);
    }

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;
    @Override
    public List<AttrEntity> queryAttrehtitesWithId(Map<String, Object> params, Integer attrGroupId) {
        //根据attrGroupId中间表查询attrId
        List<AttrAttrgroupRelationEntity> aagr = attrAttrgroupRelationDao.selectList(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId)
        );
        //根据aagr集合查询属性
        List<AttrEntity> attrEntities = aagr.stream().map((attrAttrgroupRelationEntity) ->{
            AttrEntity attr=attrService.getById(attrAttrgroupRelationEntity.getAttrId());
            return attr;
        }).collect(Collectors.toList());
        return attrEntities;
    }

    @Autowired
    private AttrGroupDao attrGroupDao;
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrs(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );
        List<AttrGroupWithAttrsVo> list= attrGroupEntities.stream().map((entity) ->{
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(entity,vo);
            List<AttrAttrgroupRelationEntity> relatiobList = attrAttrgroupRelationDao.selectList(
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id",entity.getAttrGroupId())
            );
            List<AttrEntity> attrs = relatiobList.stream().map(relation -> {
                AttrEntity attr = new AttrEntity();
                attr = attrService.getOne(
                        new QueryWrapper<AttrEntity>().eq("attr_id", relation.getAttrId())
                );
                return attr;
            }).collect(Collectors.toList());
            vo.setAttrs(attrs);
            return vo;
        }).collect(Collectors.toList());
        return list;
    }
}